#include "cell_main.h"
#include "ui_cell_main.h"

#include <QPushButton>

Cell_Main::Cell_Main(QWidget *parent)
    : QMainWindow(parent),
    ui(new Ui::Cell_Main),
    m_usrPage(nullptr),
    m_bookPage(nullptr),
    m_recordPage(nullptr)
{
    ui->setupUi(this);
    initPage();
}

void Cell_Main::initPage()
{
    m_usrPage = new Cell_UsrMgr(this);
    m_bookPage = new Cell_BookMgr(this);
    m_recordPage = new Cell_Record(this);
    ui->stackedWidget->addWidget(m_usrPage);
    ui->stackedWidget->addWidget(m_bookPage);
    ui->stackedWidget->addWidget(m_recordPage);
    ui->stackedWidget->setCurrentIndex(0);

    //按钮相响应逻辑 : 结合按钮组
    auto l = ui->tool->children();
    for(auto it : l)
    {
        if(it->objectName().contains("btn")) {
            connect(static_cast<QPushButton*>(it), &QPushButton::clicked, this, &Cell_Main::dealMenu);
        }
    }
    m_usrPage->initPage(); //初始化用户管理界面
}

void Cell_Main::dealMenu()
{
    //接收信号
    auto str = sender()->objectName();
    do
    {
        if(str == "btn_usr")
        {
            m_usrPage->initPage(); //初始化界面
            ui->stackedWidget->setCurrentIndex(0); //转换页面
            break;
        }
        else if(str == "btn_book")
        {
            m_bookPage->initPage();
            ui->stackedWidget->setCurrentIndex(1);
            break;
        }
        else if(str == "btn_record")
        {
            m_recordPage->initPage();
            ui->stackedWidget->setCurrentIndex(2);
            break;
        } else {}
    }while(false);

}

Cell_Main::~Cell_Main()
{
    delete ui;
}
